const config = require("../config");
const model = require("../model");
const Delivery = model.Delivery;


module.exports = {
    create: async (delivery) => {
        let result = await Delivery.create(delivery);
        if (result) {
            return {
                "result": true,
                "data": result
            };
        } else return { "result": false, }
    },

    getByName: async (deliveryName) => {
        let result = await Delivery.findOne(
            { "where": { "deliveryName": deliveryName } }
        );
        if (result) {
            return {
                "result": true,
                "data": result
            };
        } else return { "result": false, }
    },

    createByName: async (deliveryName) => {
        let result = await Delivery.create({
            "deliveryName": deliveryName,
            "state": "INBASE",
            "info": ""
        });
        if (result) {
            return {
                "result": true,
                "data": result
            };
        } else return { "result": false, }
    },

    getAll: async () => {
        let result = await Delivery.findAll({
            "order": [['createdAt', 'DESC']]
        });
        if (result) {
            return {
                "result": true,
                "data": result
            };
        } else return { "result": false, }
    },

    getByPage: async (page, maxPageObj) => {
        let result = await Delivery.findAndCountAll({
            order: [['createdAt', 'DESC']],
            offset: (page - 1) * maxPageObj,
            limit: maxPageObj
        });
        if (result) {
            return {
                "result": true,
                "data": result
            };
        } else return { "result": false, }
    },

    updateInfoByName: async (deliveryName, info) => {
        let result = await Delivery.update(
            { "info": info },
            { "where": { "deliveryName": deliveryName } }
        );
        if (result[0] === 1) {
            return {
                "result": true,
            };
        } else return { "result": false, }
    },

    updateStateByName: async (deliveryName, state) => {
        let result = await Delivery.update(
            { "state": state },
            { "where": { "deliveryName": deliveryName } }
        );
        if (result[0] === 1) {
            return {
                "result": true,
            };
        } else 
        {
            console.log(result);
            
            return { "result": false, }
        }
    },

    deleteByName: async (deliveryName) => {
        if (await Delivery.destroy({ where: { "deliveryName": deliveryName } })) {
            return {
                "result": true,
            };
        } else return { "result": false, }
    },

    count: async () => {
        let result = await Delivery.count(
            { where: {} }
        );
        if (result) {
            return {
                "result": true,
                "data": result
            };
        } else return { "result": false, }
    },

}
